package com.sl.au.controller;

import com.sl.au.entity.*;
import com.sl.au.service.*;
import org.hibernate.jpa.criteria.expression.function.AggregationFunction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 应收账登记收账及登记开票控制器
 */
@RestController
@RequestMapping(value="/ui/payment")
public class PaymentController extends BaseController{
    @Autowired
    ContractService contractService;
    @Autowired
    PaymentInitialService paymentInitialService;
    
    @Autowired
    ReceivedPaymentDetailInfoService receivedPaymentDetailInfoService;//回款
    @Autowired
    ReceivedPaymentDetailInfoViewService receivedPaymentDetailInfoViewService;//回款
    
    @Autowired
    InvoiceDetailInfoService invoiceDetailInfoService;//开票
    @Autowired
    InvoiceDetailInfoViewService invoiceDetailInfoViewService;//开票
    
    @Autowired
    InvoiceTotalInfoService invoiceTotalInfoService;
    @Autowired
    UserService userService;
    
    @Autowired
    ReceivedPaymentTotalInfoService receivedPaymentTotalInfoService;
    @Autowired
    PayPlanService payPlanService;
    @Autowired
    OptionService optionService;
    @Autowired
    OperateLogService operateLogService;
    @Autowired
    AccountsReceivablePlanService accountsReceivablePlanService;
    @Autowired
    AaccountsReceivableAccorspandingContractService aaccountsReceivableAccorspandingContractService;
    /**
     * 收账开票登记页面显示
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView index(){
        Module module = this.getPageModule("paymentinfo");
        ModelAndView modelAndView=createLayoutView("payment/paymentRegist");
        
        modelAndView.addObject("module", module);        
        return modelAndView;
    }

    /**
     * 修改计划页面显示
     * @param contractId
     * @param contractType
     * @return
     */
    @RequestMapping(value = "/editSchedule/{contractId}/{contractType}",method = RequestMethod.GET)
    public ModelAndView editSchedule(@PathVariable String contractId,
                                     @PathVariable String contractType){
        ModelAndView modelAndView=createLayoutView("payment/contractInfo");
        if (contractType.equals("备件合同")) {
            ContractPart contractPart = null;
            if (contractId != null && !contractId.equals("")) {
                contractPart = contractService.getContractPartById(contractId);
            }
            modelAndView.addObject("contract", contractPart);
        }else if(contractType.equals("备泵合同")){
            ContractPump contractPump = null;
            if(contractId !=null && !contractId.equals((""))){
                contractPump =contractService.getContractPumpById(contractId);
            }
            modelAndView.addObject("contract",contractPump);
        }else if(contractType.equals("维修合同")){
            ContractRepair contractRepair = null;
            if(contractId !=null && !contractId.equals("")){
                contractRepair =contractService.getContractRepairById(contractId);
            }
            modelAndView.addObject("contract",contractRepair);
        }else{
            Contract contract = null;
            if(contractId !=null && !contractId.equals("")){
                contract =contractService.getContractById(contractId);
            }
            modelAndView.addObject("contract",contract);
        }
        return modelAndView;
    }
    /**
     * 收账登记页面显示
     * @return
     */
    @RequestMapping(value = "/registPaySee/{contractId}/{contractType}",method = RequestMethod.GET)
    public ModelAndView registPaySee(@PathVariable String contractId,
                                  @PathVariable String contractType){
            ModelAndView modelAndView = createLayoutView("accountsreceivable/accountsreceivable-audit-view");
            List<Option> states = optionService.getRepository().findByGroup("回款方式");
            modelAndView.addObject("states", states);
            Account a = this.getMyAccount();
            String userId = a.getUser().getId();
            modelAndView.addObject("userId", userId);
            String orderArea=null;
            if (contractType.equals("备件合同")) {
                ContractPart contractPart = null;
                if (contractId != null && !contractId.equals("")) {
                    contractPart = contractService.getContractPartById(contractId);
                    orderArea=contractPart.getOrderArea();
                }
                modelAndView.addObject("contract", contractPart);
            } else if (contractType.equals("备泵合同")) {
                ContractPump contractPump = null;
                if (contractId != null && !contractId.equals((""))) {
                    contractPump = contractService.getContractPumpById(contractId);
                    orderArea=contractPump.getOrderArea();
                }
                modelAndView.addObject("contract", contractPump);
            } else if (contractType.equals("维修合同")) {
                ContractRepair contractRepair = null;
                if (contractId != null && !contractId.equals("")) {
                    contractRepair = contractService.getContractRepairById(contractId);
                    orderArea=contractRepair.getOrderArea();
                }
                modelAndView.addObject("contract", contractRepair);
            } else {
                Contract contract = null;
                List<DivideScale> divideScales = null;
                if (contractId != null && !contractId.equals("")) {
                    contract = contractService.getContractById(contractId);
                    orderArea=contract.getOrderArea();
                    divideScales = contractService.getContractDivideScales(0, 1000, contractId, null, null);
                }
                if(divideScales.size()==0){
                	modelAndView.addObject("divideScales",0);
                }else{
                	modelAndView.addObject("divideScales",1);
                }
                modelAndView.addObject("contract", contract);
            }
            String userSuperior=a.getUser().getSaleArea().getSuperior();
            List<User> userList=paymentInitialService.getRepository().getUserList(userSuperior);
            modelAndView.addObject("users", userList);
//            String userArea=a.getUser().getSaleArea().getSuperiorArea();
//            List<Account> paymentUserList=paymentInitialService.getRepository().getUserList(userArea,role);
//            modelAndView.addObject("paymentUsers", paymentUserList);
            
            float[] paidSum=receivedPaymentDetailInfoService.getRepository().getnReceivedPaymentDetailPrice(contractId); 
            float sum=0;
            for(int i=0;i<paidSum.length;i++){
                sum=sum+paidSum[i];
            }
            float[] paymentSum=invoiceDetailInfoService.getRepository().getnInvoiceDetailPrice(contractId);
            float sums=0;
            for(int i=0;i<paymentSum.length;i++){
                sums=sums+paymentSum[i];
            }
            //List<AccountsReceivablePlan> accountsReceivablePlan =accountsReceivablePlanService.getRepository().findBySContractIdAndSContractType(contractId, contractType);
            float[] ScheduleSum= accountsReceivablePlanService.getRepository().getnTotalPrice(contractId, contractType);
            float sumScheduleSum=0;
            for (int i = 0; i < ScheduleSum.length; i++) {                
                sumScheduleSum+=ScheduleSum[i];
            }
            modelAndView.addObject("sumScheduleSum", sumScheduleSum);
            modelAndView.addObject("paymentSum", sums);
            modelAndView.addObject("paidSum", sum);
            return modelAndView;
    }
    /**
     * 收账登记页面显示
     * @return
     */
    @RequestMapping(value = "/registPay/{contractId}/{contractType}",method = RequestMethod.GET)
    public ModelAndView registPay(@PathVariable String contractId,
    		@PathVariable String contractType){
    	ModelAndView modelAndView = createLayoutView("payment/registPay");
    	List<Option> states = optionService.getRepository().findByGroup("回款方式");
    	modelAndView.addObject("states", states);
    	Account a = this.getMyAccount();
    	User user = a.getUser();
    	String userId = user.getId();
    	modelAndView.addObject("userId", userId);
    	modelAndView.addObject("user", user);
    	String orderArea=null;
    	if (contractType.equals("备件合同")) {
    		ContractPart contractPart = null;
    		if (contractId != null && !contractId.equals("")) {
    			contractPart = contractService.getContractPartById(contractId);
    			orderArea=contractPart.getOrderArea();
    		}
    		modelAndView.addObject("contract", contractPart);
    	} else if (contractType.equals("备泵合同")) {
    		ContractPump contractPump = null;
    		if (contractId != null && !contractId.equals((""))) {
    			contractPump = contractService.getContractPumpById(contractId);
    			orderArea=contractPump.getOrderArea();
    		}
    		modelAndView.addObject("contract", contractPump);
    	} else if (contractType.equals("维修合同")) {
    		ContractRepair contractRepair = null;
    		if (contractId != null && !contractId.equals("")) {
    			contractRepair = contractService.getContractRepairById(contractId);
    			orderArea=contractRepair.getOrderArea();
    		}
    		modelAndView.addObject("contract", contractRepair);
    	} else {
    		Contract contract = null;
    		List<DivideScale> divideScales = null;
    		if (contractId != null && !contractId.equals("")) {
    			contract = contractService.getContractById(contractId);
    			orderArea=contract.getOrderArea();
    			divideScales = contractService.getContractDivideScales(0, 1000, contractId, null, null);
    		}
    		if(divideScales.size()==0){
    			modelAndView.addObject("divideScales",0);
    		}else{
    			modelAndView.addObject("divideScales",1);
    		}
    		modelAndView.addObject("contract", contract);
    	}
    	String userSuperior=a.getUser().getSaleArea().getSuperior();
    	List<User> userList=paymentInitialService.getRepository().getUserList(userSuperior);
    	modelAndView.addObject("users", userList);
//            String userArea=a.getUser().getSaleArea().getSuperiorArea();
//            List<Account> paymentUserList=paymentInitialService.getRepository().getUserList(userArea,role);
//            modelAndView.addObject("paymentUsers", paymentUserList);
    	float[] paidSum=receivedPaymentDetailInfoService.getRepository().getnReceivedPaymentDetailPrice(contractId); 
        float sum=0;
        for(int i=0;i<paidSum.length;i++){
            sum=sum+paidSum[i];
        }
        float[] paymentSum=invoiceDetailInfoService.getRepository().getnInvoiceDetailPrice(contractId);
        float sums=0;
        for(int i=0;i<paymentSum.length;i++){
            sums=sums+paymentSum[i];
        }
        //List<AccountsReceivablePlan> accountsReceivablePlan =accountsReceivablePlanService.getRepository().findBySContractIdAndSContractType(contractId, contractType);
        float[] ScheduleSum= accountsReceivablePlanService.getRepository().getnTotalPrice(contractId, contractType);
        float sumScheduleSum=0;
        for (int i = 0; i < ScheduleSum.length; i++) {                
            sumScheduleSum+=ScheduleSum[i];
        }
    	modelAndView.addObject("sumScheduleSum", sumScheduleSum);
    	modelAndView.addObject("paymentSum", sums);
    	modelAndView.addObject("paidSum", sum);
    	return modelAndView;
    }
    /**
     * 开票登记页面显示
     * @return
     */
    @RequestMapping(value = "/registPayment/{contractId}/{contractType}",method = RequestMethod.GET)
    public ModelAndView registPayment(@PathVariable String contractId,
                                      @PathVariable String contractType){
        Account a = this.getMyAccount();
       //Module module = this.getPageModule("");
        if(a.getRole().getName().equals("财务人员")){
            ModelAndView modelAndView=createLayoutView("payment/registPaymentFinance");
            if (contractType.equals("备件合同")){
                ContractPart contractPart = null;
                if (contractId != null && !contractId.equals("")) {
                    contractPart = contractService.getContractPartById(contractId);
                }
                modelAndView.addObject("contract", contractPart);
            }else if(contractType.equals("备泵合同")){
                ContractPump contractPump = null;
                if(contractId !=null && !contractId.equals((""))){
                    contractPump =contractService.getContractPumpById(contractId);
                }
                modelAndView.addObject("contract",contractPump);
            }else if(contractType.equals("维修合同")){
                ContractRepair contractRepair = null;
                if(contractId !=null && !contractId.equals("")){
                    contractRepair =contractService.getContractRepairById(contractId);
                }
                modelAndView.addObject("contract",contractRepair);
            }else{
                Contract contract = null;
                if(contractId !=null && !contractId.equals("")){
                    contract =contractService.getContractById(contractId);
                }
                modelAndView.addObject("contract",contract);
            }
            //float[] paidSum=paymentInitialService.getPayRegistRepository().getPaySum(contractId);//回款
            float[] paidSum=receivedPaymentDetailInfoService.getRepository().getnReceivedPaymentDetailPrice(contractId);
            float sum=0;
            for(int i=0;i<paidSum.length;i++){
                sum=sum+paidSum[i];
            }
            //float[] paymentSum=paymentInitialService.getPaymentRegistRepository().getPaymentSum(contractId);
            float[] paymentSum=invoiceDetailInfoService.getRepository().getPaymentSum(contractId);
            float sums=0;
            for(int i=0;i<paymentSum.length;i++){
                sums=sums+paymentSum[i];
            }
            modelAndView.addObject("paymentSum", sums);
            modelAndView.addObject("paidSum", sum);
            return modelAndView;
        }
        else {
            ModelAndView modelAndView = createLayoutView("payment/registPayment");
            List<Option> states = optionService.getRepository().findByGroup("回款方式");
            modelAndView.addObject("states", states);
            String orderArea=null;
            if (contractType.equals("备件合同")) {
                ContractPart contractPart = null;
                if (contractId != null && !contractId.equals("")) {
                    contractPart = contractService.getContractPartById(contractId);
                    orderArea=contractPart.getOrderArea();
                }
                modelAndView.addObject("contract", contractPart);
            } else if (contractType.equals("备泵合同")) {
                ContractPump contractPump = null;
                if (contractId != null && !contractId.equals((""))) {
                    contractPump = contractService.getContractPumpById(contractId);
                    orderArea=contractPump.getOrderArea();
                }
                modelAndView.addObject("contract", contractPump);
            } else if (contractType.equals("维修合同")) {
                ContractRepair contractRepair = null;
                if (contractId != null && !contractId.equals("")) {
                    contractRepair = contractService.getContractRepairById(contractId);
                    orderArea=contractRepair.getOrderArea();
                }
                modelAndView.addObject("contract", contractRepair);
            } else {
                Contract contract = null;
                if (contractId != null && !contractId.equals("")) {
                    contract = contractService.getContractById(contractId);
                    orderArea=contract.getOrderArea();
                }
                modelAndView.addObject("contract", contract);
            }
            User user = a.getUser();
            String userId = user.getId();
            modelAndView.addObject("userId", userId);
            modelAndView.addObject("user", user);
            String userSuperior=a.getUser().getSaleArea().getSuperior();
            List<User> userList=paymentInitialService.getRepository().getUserList(userSuperior);
            modelAndView.addObject("users", userList);
//            List<Account> paymentUserList=paymentInitialService.getRepository().getUserList(userArea);
//            modelAndView.addObject("paymentUsers", paymentUserList);
            //float[] paidSum = paymentInitialService.getPayRegistRepository().getPaySum(contractId);
            float[] paidSum=receivedPaymentDetailInfoService.getRepository().getnReceivedPaymentDetailPrice(contractId);
            float sum = 0;
            for (int i = 0; i < paidSum.length; i++) {
                sum = sum + paidSum[i];
            }
            //float[] paymentSum = paymentInitialService.getPaymentRegistRepository().getPaymentSum(contractId);
            float[] paymentSum=invoiceDetailInfoService.getRepository().getPaymentSum(contractId);
            float sums = 0;
            for (int i = 0; i < paymentSum.length; i++) {
                sums = sums + paymentSum[i];
            }
            List<AccountsReceivablePlan> paymentSchedules =accountsReceivablePlanService.getRepository().findBySContractIdAndSContractType(contractId, contractType);
            float scheduleSum[] = new float[paymentSchedules.size()];
            float sumScheduleSum=0;
            for (int n = 0; n < paymentSchedules.size(); n++) {
                scheduleSum[n] = (float) paymentSchedules.get(n).getnTotalPrice();
                sumScheduleSum=sumScheduleSum+scheduleSum[n];
            }
            modelAndView.addObject("sumScheduleSum", sumScheduleSum);
            modelAndView.addObject("paymentSum", sums);
            modelAndView.addObject("paidSum", sum);
            return modelAndView;
        }
    }
    /**
     * 开票登记页面显示-财务
     * @return
     */
    @RequestMapping(value = "/registPaymentFinance/{contractId}/{contractType}",method = RequestMethod.GET)
    public ModelAndView registPaymentFinance(@PathVariable String contractId,
                                             @PathVariable String contractType){
        ModelAndView modelAndView=createLayoutView("payment/registPaymentFinance");
        if (contractType.equals("备件合同")) {
            ContractPart contractPart = null;
            if (contractId != null && !contractId.equals("")) {
                contractPart = contractService.getContractPartById(contractId);
            }
            modelAndView.addObject("contract", contractPart);
        }else if(contractType.equals("备泵合同")){
            ContractPump contractPump = null;
            if(contractId !=null && !contractId.equals((""))){
                contractPump =contractService.getContractPumpById(contractId);
            }
            modelAndView.addObject("contract",contractPump);
        }else if(contractType.equals("维修合同")){
            ContractRepair contractRepair = null;
            if(contractId !=null && !contractId.equals("")){
                contractRepair =contractService.getContractRepairById(contractId);
            }
            modelAndView.addObject("contract",contractRepair);
        }else{
            Contract contract = null;
            if(contractId !=null && !contractId.equals("")){
                contract =contractService.getContractById(contractId);
            }
            modelAndView.addObject("contract",contract);
        }
        return modelAndView;
    }

    /**
     * 判断是否初始化
     * @return
     */
    @RequestMapping(value ="/isInitial/{contractId}/{contractType}",method = RequestMethod.GET)
    public Map<String,Object> isInitial(@PathVariable String contractId,
                                      @PathVariable String contractType) {
        Map<String, Object> map = new HashMap<>();
        //List<PaymentInitial> paymentSchedules = paymentInitialService.getRepository().findByContractIdAndContractType(contractId, contractType);
        List<AccountsReceivablePlan> AccountsReceivablePlans = accountsReceivablePlanService.getRepository().findBySContractIdAndSContractType(contractId, contractType);
        float scheduleSum[] = new float[AccountsReceivablePlans.size()];
        float sumScheduleSum = 0;
        if(AccountsReceivablePlans.size()==0){
            map.put("code", 0);
        }
        else {
            for (int n = 0; n < AccountsReceivablePlans.size(); n++) {
                scheduleSum[n] = (float) AccountsReceivablePlans.get(n).getnTotalPrice();
                sumScheduleSum = sumScheduleSum + scheduleSum[n];
            }
            if (sumScheduleSum == 0) {
                map.put("code", 0);
            }
            else {
                map.put("scheduleSum",sumScheduleSum);
                float[] paidSum = paymentInitialService.getPayRegistRepository().getPaySum(contractId);
                if (paidSum.length == 0) {
                    map.put("paidSum", 0);
                } else {
                    float paidSums = 0;
                    for (int i = 0; i < paidSum.length; i++) {
                        paidSums = paidSums + paidSum[i];
                    }
                    map.put("paidSum", paidSums);
                }
                float[] paymentSum = paymentInitialService.getPaymentRegistRepository().getPaymentSum(contractId);
                if (paymentSum.length == 0) {
                    map.put("paymentSum", 0);
                } else {
                    float paymentSums = 0;
                    for (int i = 0; i < paymentSum.length; i++) {
                        paymentSums = paymentSums + paymentSum[i];
                    }
                    map.put("paymentSum", paymentSums);
                }
            }
        }
        return  map;
    }
    /**
     * 新增保存回款
     * @param payRegist
     * @return
     */
    @RequestMapping(value ="/savePay",method = RequestMethod.POST)
    public Map<String,Object> savePay(@RequestBody ReceivedPaymentDetailInfo payRegist) {
        Map<String, Object> map = new HashMap<>();
        String name = request.getParameter("name");
        Account a = this.getMyAccount();
        User user = a.getUser();
        payRegist.setsCreateUserId(user.getEmpLogin().getEmpId());
        Option sReceivedPaymentTradeWay = optionService.getRepository().findOne(payRegist.getsReceivedPaymentTradeWay().getId());
        ReceivedPaymentTotalInfo sReceivedPaymentTotalId = receivedPaymentTotalInfoService.getRepository().findOne(payRegist.getsReceivedPaymentTotalId().getId());
        payRegist.setsReceivedPaymentTradeWay(sReceivedPaymentTradeWay);
        payRegist.setsReceivedPaymentTotalId(sReceivedPaymentTotalId);
        receivedPaymentDetailInfoService.getRepository().save(payRegist);
        
        AaccountsReceivableAccorspandingContract aaccountsReceivableAccorspandingContract = aaccountsReceivableAccorspandingContractService.getRepository().findBySContractIdAndSContractTypeAndNContractOrgType(payRegist.getsContractId(), payRegist.getsContractType(), 1);
        if(aaccountsReceivableAccorspandingContract == null){
    		AaccountsReceivableAccorspandingContract newAaccountsReceivableAccorspandingContract = new AaccountsReceivableAccorspandingContract();
    		newAaccountsReceivableAccorspandingContract.setnContractOrgType(1);
    		newAaccountsReceivableAccorspandingContract.setnIsUpdate(0);
    		newAaccountsReceivableAccorspandingContract.setnIsUpdateForTotal(0);
    		newAaccountsReceivableAccorspandingContract.setsContractId(payRegist.getsContractId());
    		newAaccountsReceivableAccorspandingContract.setsContractType(payRegist.getsContractType());
    		aaccountsReceivableAccorspandingContractService.getRepository().save(newAaccountsReceivableAccorspandingContract);
    	}else{
    		aaccountsReceivableAccorspandingContract.setnContractOrgType(1);
    		aaccountsReceivableAccorspandingContract.setnIsUpdate(0);
    		aaccountsReceivableAccorspandingContract.setnIsUpdateForTotal(0);
    		aaccountsReceivableAccorspandingContract.setsContractId(payRegist.getsContractId());
    		aaccountsReceivableAccorspandingContract.setsContractType(payRegist.getsContractType());
    		aaccountsReceivableAccorspandingContractService.getRepository().save(aaccountsReceivableAccorspandingContract);
    	}
        
        String contractId = payRegist.getsContractId(); 
        String contractType = payRegist.getsContractType();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        BigDecimal bigDecimal = new BigDecimal(payRegist.getnReceivedPaymentDetailPrice());
        String content = "";
        
        //String t   =   payRegist.getsReceivedPaymentPersonId();
        if (contractType.equals("项目合同")){
			Contract old = contractService.getRepository().findOne(contractId);
			content = "新增保存回款【"+"合同号:"+old.getSupplierCode()+",回款人"+name+",金额:"+bigDecimal.toString()+",时间:"+formatter.format(payRegist.getdReceivedPaymentPlanAccDate())+"】";
		}else if (contractType.equals("备件合同")){
			ContractPart old = contractService.getRepository2().findOne(contractId);
			content = "新增保存回款【"+"合同号:"+old.getSupplierCode()+",回款人"+name+",金额:"+bigDecimal.toString()+",时间:"+formatter.format(payRegist.getdReceivedPaymentPlanAccDate())+"】";
		}else if (contractType.equals("备泵合同")){
			ContractPump old = contractService.getRepository3().findOne(contractId);
			content = "新增保存回款【"+"合同号:"+old.getSupplierCode()+",回款人"+name+",金额:"+bigDecimal.toString()+",时间:"+formatter.format(payRegist.getdReceivedPaymentPlanAccDate())+"】";
		}else if (contractType.equals("维修合同")){
			ContractRepair old = contractService.getRepository4().findOne(contractId);
			content = "新增保存回款【"+"合同号:"+old.getSupplierCode()+",回款人"+name+",金额:"+bigDecimal.toString()+",时间:"+formatter.format(payRegist.getdReceivedPaymentPlanAccDate())+"】";
		}
        operateLogService.log(user, "新增保存回款", content);
        String payRegistId = payRegist.getId();
        Date createTime = payRegist.getCreateTime();
        float[] paidSum=receivedPaymentDetailInfoService.getRepository().getnReceivedPaymentDetailPrice(contractId); 
        float sum=0;
        for(int i=0;i<paidSum.length;i++){
            sum=sum+paidSum[i];
        }
        map.put("payRegistId", payRegistId);
        map.put("sum", sum);
        map.put("code", 1);
        map.put("createTime", createTime);
        return map;
    }
    /**
     * 编辑保存回款
     * @param payRegist
     * @return
     */
    @RequestMapping(value ="/upDataPay",method = RequestMethod.POST)
    public Map<String,Object> updataPay(@RequestBody ReceivedPaymentDetailInfo payRegist){
            Map<String,Object> map=new HashMap<>();
            String name = request.getParameter("name");
            Account account = this.getMyAccount();
            User user = account.getUser();
            ReceivedPaymentDetailInfo payRegist1=receivedPaymentDetailInfoService.getRepository().findOne(payRegist.getId());
            Option sReceivedPaymentTradeWay = optionService.getRepository().findOne(payRegist.getsReceivedPaymentTradeWay().getId());
            ReceivedPaymentTotalInfo sReceivedPaymentTotalId = receivedPaymentTotalInfoService.getRepository().findOne(payRegist.getsReceivedPaymentTotalId().getId());
            //float paidSum=payRegist1.getPaidSum();
            //Date paidDateOld=payRegist1.getPaidDate();
            payRegist1.setsReceivedPaymentMemo(payRegist.getsReceivedPaymentMemo());
            payRegist1.setsCreateUserId(user.getEmpLogin().getEmpId());
            payRegist1.setnReceivedPaymentDetailPrice(payRegist.getnReceivedPaymentDetailPrice());
            payRegist1.setsReceivedPaymentTradeWay(sReceivedPaymentTradeWay);
            payRegist1.setdReceivedPaymentPlanAccDate(payRegist.getdReceivedPaymentPlanAccDate());
            payRegist1.setsContractId(payRegist.getsContractId());
            payRegist1.setsContractType(payRegist.getsContractType());
            payRegist1.setsReceivedPaymentTotalId(sReceivedPaymentTotalId);
            payRegist1.setsReceivedPaymentPersonId(payRegist.getsReceivedPaymentPersonId());
            payRegist1.setdReceivedPaymentActualAccDate(payRegist.getdReceivedPaymentActualAccDate());
            receivedPaymentDetailInfoService.getRepository().save(payRegist1);
            AaccountsReceivableAccorspandingContract aaccountsReceivableAccorspandingContract = aaccountsReceivableAccorspandingContractService.getRepository().findBySContractIdAndSContractTypeAndNContractOrgType(payRegist.getsContractId(), payRegist.getsContractType(), 1);
            if(aaccountsReceivableAccorspandingContract == null){
        		AaccountsReceivableAccorspandingContract newAaccountsReceivableAccorspandingContract = new AaccountsReceivableAccorspandingContract();
        		newAaccountsReceivableAccorspandingContract.setnContractOrgType(1);
        		newAaccountsReceivableAccorspandingContract.setnIsUpdate(0);
        		newAaccountsReceivableAccorspandingContract.setnIsUpdateForTotal(0);
        		newAaccountsReceivableAccorspandingContract.setsContractId(payRegist.getsContractId());
        		newAaccountsReceivableAccorspandingContract.setsContractType(payRegist.getsContractType());
        		aaccountsReceivableAccorspandingContractService.getRepository().save(newAaccountsReceivableAccorspandingContract);
        	}else{
        		aaccountsReceivableAccorspandingContract.setnContractOrgType(1);
        		aaccountsReceivableAccorspandingContract.setnIsUpdate(0);
        		aaccountsReceivableAccorspandingContract.setnIsUpdateForTotal(0);
        		aaccountsReceivableAccorspandingContract.setsContractId(payRegist.getsContractId());
        		aaccountsReceivableAccorspandingContract.setsContractType(payRegist.getsContractType());
        		aaccountsReceivableAccorspandingContractService.getRepository().save(aaccountsReceivableAccorspandingContract);
        	}
            String contractId = payRegist.getsContractId();
            String contractType = payRegist.getsContractType();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            BigDecimal bigDecimal = new BigDecimal(payRegist.getnReceivedPaymentDetailPrice());
            String content = "";
            if (contractType.equals("项目合同")){
    			Contract old = contractService.getRepository().findOne(contractId);
    			content = "编辑回款【"+"合同号:"+old.getSupplierCode()+",回款人"+name+",金额:"+bigDecimal.toString()+",时间:"+formatter.format(payRegist.getdReceivedPaymentPlanAccDate())+"】";
    		}else if (contractType.equals("备件合同")){
    			ContractPart old = contractService.getRepository2().findOne(contractId);
    			content = "编辑回款【"+"合同号:"+old.getSupplierCode()+",回款人"+name+",金额:"+bigDecimal.toString()+",时间:"+formatter.format(payRegist.getdReceivedPaymentPlanAccDate())+"】";
    		}else if (contractType.equals("备泵合同")){
    			ContractPump old = contractService.getRepository3().findOne(contractId);
    			content = "编辑回款【"+"合同号:"+old.getSupplierCode()+",回款人"+name+",金额:"+bigDecimal.toString()+",时间:"+formatter.format(payRegist.getdReceivedPaymentPlanAccDate())+"】";
    		}else if (contractType.equals("维修合同")){
    			ContractRepair old = contractService.getRepository4().findOne(contractId);
    			content = "编辑回款【"+"合同号:"+old.getSupplierCode()+",回款人"+name+",金额:"+bigDecimal.toString()+",时间:"+formatter.format(payRegist.getdReceivedPaymentPlanAccDate())+"】";
    		}
            operateLogService.log(user, "编辑回款", content);
            String payRegistId = payRegist.getId();
            Date createTime = payRegist.getCreateTime();
            float[] paidSum=receivedPaymentDetailInfoService.getRepository().getnReceivedPaymentDetailPrice(contractId); 
            float sum=0;
            for(int i=0;i<paidSum.length;i++){
                sum=sum+paidSum[i];
            }
            map.put("sum", sum);
            map.put("code", 1);
            map.put("payRegistId", payRegistId);
            map.put("createTime", createTime);
            return map;
    }
    /**
     * 删除回款
     * @return
     */
    @RequestMapping(value ="/deletePay",method = RequestMethod.GET)
    public Map<String,Object> deletePay(HttpServletRequest request){
    	Map<String, Object> map = new HashMap<String, Object>();
        String ids = request.getParameter("ids");
        String id[] = ids.split(",");
        List<ReceivedPaymentDetailInfo> receivedPaymentDetailInfos = new ArrayList<>();
        /*List<PaymentPlan> changePaymentPaln=null;
        List<Date> paymentRegistList = new ArrayList<>();
        List<Date> getPaymentPaln=new ArrayList<>();*/
        String contractId=null;
        String contractType=null;
        for (int i = 0; i < id.length; i++){
            String paymentRegistId = id[i];
            ReceivedPaymentDetailInfo receivedPaymentDetailInfo = new ReceivedPaymentDetailInfo();
            receivedPaymentDetailInfo.setId(paymentRegistId);
            receivedPaymentDetailInfos.add(receivedPaymentDetailInfo);
            receivedPaymentDetailInfo=receivedPaymentDetailInfoService.getRepository().findOne(paymentRegistId);
            contractId=receivedPaymentDetailInfo.getsContractId();
            contractType=receivedPaymentDetailInfo.getsContractType();
        }
        receivedPaymentDetailInfoService.getRepository().deleteInBatch(receivedPaymentDetailInfos);
        AaccountsReceivableAccorspandingContract aaccountsReceivableAccorspandingContract = aaccountsReceivableAccorspandingContractService.getRepository().findBySContractIdAndSContractTypeAndNContractOrgType(contractId, contractType, 2);
        if(aaccountsReceivableAccorspandingContract == null){
    		AaccountsReceivableAccorspandingContract newAaccountsReceivableAccorspandingContract = new AaccountsReceivableAccorspandingContract();
    		newAaccountsReceivableAccorspandingContract.setnContractOrgType(2);
    		newAaccountsReceivableAccorspandingContract.setnIsUpdate(0);
    		newAaccountsReceivableAccorspandingContract.setnIsUpdateForTotal(0);
    		newAaccountsReceivableAccorspandingContract.setsContractId(contractId);
    		newAaccountsReceivableAccorspandingContract.setsContractType(contractType);
    		aaccountsReceivableAccorspandingContractService.getRepository().save(newAaccountsReceivableAccorspandingContract);
    	}else{
    		aaccountsReceivableAccorspandingContract.setnContractOrgType(2);
    		aaccountsReceivableAccorspandingContract.setnIsUpdate(0);
    		aaccountsReceivableAccorspandingContract.setnIsUpdateForTotal(0);
    		aaccountsReceivableAccorspandingContract.setsContractId(contractId);
    		aaccountsReceivableAccorspandingContract.setsContractType(contractType);
    		aaccountsReceivableAccorspandingContractService.getRepository().save(aaccountsReceivableAccorspandingContract);
    	}
        /////插入删除日志
        Account a = this.getMyAccount();
        User user = a.getUser();
        String contractId1 = "";
        String contractType1 = "";
        for(ReceivedPaymentDetailInfo d:receivedPaymentDetailInfos){
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            ReceivedPaymentDetailInfo oldPR=receivedPaymentDetailInfoService.getRepository().findOne(d.getId());
            contractId1 = oldPR.getsContractId();
            contractType1 = oldPR.getsContractType();
            User sReceivedPaymentPerson = userService.findByEmpId(oldPR.getsReceivedPaymentPersonId());
            BigDecimal bigDecimal = new BigDecimal(oldPR.getnReceivedPaymentDetailPrice());
            String content = "";
            if (contractType1.equals("项目合同")){
    			Contract old = contractService.getRepository().findOne(contractId1);
    			content = "删除回款【"+"合同号:"+old.getSupplierCode()+",回款人"+sReceivedPaymentPerson.getEmpLogin().getName()+",金额:"+bigDecimal.toString()+",时间:"+formatter.format(oldPR.getdReceivedPaymentPlanAccDate())+"】";
    		}else if (contractType1.equals("备件合同")){
    			ContractPart old = contractService.getRepository2().findOne(contractId1);
    			content = "删除回款【"+"合同号:"+old.getSupplierCode()+",回款人"+sReceivedPaymentPerson.getEmpLogin().getName()+",金额:"+bigDecimal.toString()+",时间:"+formatter.format(oldPR.getdReceivedPaymentPlanAccDate())+"】";
    		}else if (contractType1.equals("备泵合同")){
    			ContractPump old = contractService.getRepository3().findOne(contractId1);
    			content = "删除回款【"+"合同号:"+old.getSupplierCode()+",回款人"+sReceivedPaymentPerson.getEmpLogin().getName()+",金额:"+bigDecimal.toString()+",时间:"+formatter.format(oldPR.getdReceivedPaymentPlanAccDate())+"】";
    		}else if (contractType1.equals("维修合同")){
    			ContractRepair old = contractService.getRepository4().findOne(contractId1);
    			content = "删除回款【"+"合同号:"+old.getSupplierCode()+",回款人"+sReceivedPaymentPerson.getEmpLogin().getName()+",金额:"+bigDecimal.toString()+",时间:"+formatter.format(oldPR.getdReceivedPaymentPlanAccDate())+"】";
    		}
            operateLogService.log(user, "删除回款", content);
            payPlanService.payNotionalPooling(oldPR.getsContractId(),user.getName());
        }
        float[] paidSum=receivedPaymentDetailInfoService.getRepository().getnReceivedPaymentDetailPrice(contractId); 
        float sum=0;
        for(int i=0;i<paidSum.length;i++){
            sum=sum+paidSum[i];
        }
        map.put("sum", sum);
        map.put("code", 1);
        return map;
    	
    }
    /**
     *新增保存开票
     * @param paymentRegist
     * @return
     */
    @RequestMapping(value ="/savePayment",method = RequestMethod.POST)
    public Map<String,Object> savePayment(@RequestBody InvoiceDetailInfo invoiceDetailInfo){
        Map<String,Object> map=new HashMap<>();
        String name = request.getParameter("name");
        Account a = this.getMyAccount(); 
        User user = a.getUser();
        InvoiceTotalInfo invoiceTotalInfo = invoiceTotalInfoService.getRepository().findOne(invoiceDetailInfo.getsInvoiceTotalId().getId());
        //User sInvoicePerson = userService.getRepository().findOne(invoiceDetailInfo.getsInvoicePersonId());
        invoiceDetailInfo.setId(java.util.UUID.randomUUID().toString());
        invoiceDetailInfo.setsInvoiceTotalId(invoiceTotalInfo);
        invoiceDetailInfo.setsCreateUserId(user.getEmpLogin().getEmpId());
        //invoiceDetailInfo.setsInvoicePersonId(sInvoicePerson.getEmpLogin().getEmpId());
        invoiceDetailInfoService.getRepository().save(invoiceDetailInfo);
        AaccountsReceivableAccorspandingContract aaccountsReceivableAccorspandingContract = aaccountsReceivableAccorspandingContractService.getRepository().findBySContractIdAndSContractTypeAndNContractOrgType(invoiceDetailInfo.getsContractId(), invoiceDetailInfo.getsContractType(), 2);
        if(aaccountsReceivableAccorspandingContract == null){
    		AaccountsReceivableAccorspandingContract newAaccountsReceivableAccorspandingContract = new AaccountsReceivableAccorspandingContract();
    		newAaccountsReceivableAccorspandingContract.setnContractOrgType(2);
    		newAaccountsReceivableAccorspandingContract.setnIsUpdate(0);
    		newAaccountsReceivableAccorspandingContract.setnIsUpdateForTotal(0);
    		newAaccountsReceivableAccorspandingContract.setsContractId(invoiceDetailInfo.getsContractId());
    		newAaccountsReceivableAccorspandingContract.setsContractType(invoiceDetailInfo.getsContractType());
    		aaccountsReceivableAccorspandingContractService.getRepository().save(newAaccountsReceivableAccorspandingContract);
    	}else{
    		aaccountsReceivableAccorspandingContract.setnContractOrgType(2);
    		aaccountsReceivableAccorspandingContract.setnIsUpdate(0);
    		aaccountsReceivableAccorspandingContract.setnIsUpdateForTotal(0);
    		aaccountsReceivableAccorspandingContract.setsContractId(invoiceDetailInfo.getsContractId());
    		aaccountsReceivableAccorspandingContract.setsContractType(invoiceDetailInfo.getsContractType());
    		aaccountsReceivableAccorspandingContractService.getRepository().save(aaccountsReceivableAccorspandingContract);
    	}
        //map=paymentInitialService.setPaymentPlan(paymentRegist);
        //payPlanService.payNotionalPooling(invoiceDetailInfo.getsContractId(),user.getName());
        //paymentRegist.setUser(user);
        
        String contractId = invoiceDetailInfo.getsContractId();
        String contractType = invoiceDetailInfo.getsContractType();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        BigDecimal bigDecimal = new BigDecimal(invoiceDetailInfo.getnInvoiceDetailPrice());
        String content = "";
        if (contractType.equals("项目合同")){
			Contract old = contractService.getRepository().findOne(contractId);
			content = "新增保存开票【"+"合同号:"+old.getSupplierCode()+",开票人"+name+",金额:"+bigDecimal.toString()+",时间:"+formatter.format(invoiceDetailInfo.getdInvoicePlanAccDate())+"】";
		}else if (contractType.equals("备件合同")){
			ContractPart old = contractService.getRepository2().findOne(contractId);
			content = "新增保存开票【"+"合同号:"+old.getSupplierCode()+",开票人"+name+",金额:"+bigDecimal.toString()+",时间:"+formatter.format(invoiceDetailInfo.getdInvoicePlanAccDate())+"】";
		}else if (contractType.equals("备泵合同")){
			ContractPump old = contractService.getRepository3().findOne(contractId);
			content = "新增保存开票【"+"合同号:"+old.getSupplierCode()+",开票人"+name+",金额:"+bigDecimal.toString()+",时间:"+formatter.format(invoiceDetailInfo.getdInvoicePlanAccDate())+"】";
		}else if (contractType.equals("维修合同")){
			ContractRepair old = contractService.getRepository4().findOne(contractId);
			content = "新增保存开票【"+"合同号:"+old.getSupplierCode()+",开票人"+name+",金额:"+bigDecimal.toString()+",时间:"+formatter.format(invoiceDetailInfo.getdInvoicePlanAccDate())+"】";
		}
        operateLogService.log(user, "新增保存开票", content);
        String paymentRegisttId = invoiceDetailInfo.getId();
        Date createTime = invoiceDetailInfo.getCreateTime();
        float[] paymentSum=invoiceDetailInfoService.getRepository().getPaymentSum(contractId);
        float sums = 0;
        for (int i = 0; i < paymentSum.length; i++) {
            sums = sums + paymentSum[i];
        }
        map.put("code", 1);
        map.put("sum", sums);
        map.put("paymentRegisttId", paymentRegisttId);
        map.put("createTime", createTime);
        return map;
    }
    /**
     * 编辑保存开票
     * @param paymentRegist
     * @return
     */
    @RequestMapping(value ="/upDataPayment",method = RequestMethod.POST)
    public Map<String,Object> upDataPayment(@RequestBody InvoiceDetailInfo invoiceDetailInfo){
            Map<String,Object> map=new HashMap<>();
            String name = request.getParameter("name");
            InvoiceDetailInfo invoiceDetailInfo1=invoiceDetailInfoService.getRepository().findOne(invoiceDetailInfo.getId());
            InvoiceTotalInfo invoiceTotalInfo = invoiceTotalInfoService.getRepository().findOne(invoiceDetailInfo.getsInvoiceTotalId().getId());
            //double paymentSum=invoiceDetailInfo1.getnInvoiceDetailPrice();
            invoiceDetailInfo1.setsInvoiceMemo(invoiceDetailInfo.getsInvoiceMemo());
            invoiceDetailInfo1.setsContractType(invoiceDetailInfo.getsContractType());
            invoiceDetailInfo1.setsContractId(invoiceDetailInfo.getsContractId());
            //invoiceDetailInfo1.setdInvoiceActualAccDate(invoiceDetailInfo.getdInvoiceActualAccDate());
            invoiceDetailInfo1.setnInvoiceDetailPrice(invoiceDetailInfo.getnInvoiceDetailPrice());
            invoiceDetailInfo1.setsInvoicePersonId(invoiceDetailInfo.getsInvoicePersonId());
            invoiceDetailInfo1.setsInvoiceTotalId(invoiceTotalInfo);
            invoiceDetailInfoService.getRepository().save(invoiceDetailInfo1);
            AaccountsReceivableAccorspandingContract aaccountsReceivableAccorspandingContract = aaccountsReceivableAccorspandingContractService.getRepository().findBySContractIdAndSContractTypeAndNContractOrgType(invoiceDetailInfo.getsContractId(), invoiceDetailInfo.getsContractType(), 2);
            if(aaccountsReceivableAccorspandingContract == null){
        		AaccountsReceivableAccorspandingContract newAaccountsReceivableAccorspandingContract = new AaccountsReceivableAccorspandingContract();
        		newAaccountsReceivableAccorspandingContract.setnContractOrgType(2);
        		newAaccountsReceivableAccorspandingContract.setnIsUpdate(0);
        		newAaccountsReceivableAccorspandingContract.setnIsUpdateForTotal(0);
        		newAaccountsReceivableAccorspandingContract.setsContractId(invoiceDetailInfo.getsContractId());
        		newAaccountsReceivableAccorspandingContract.setsContractType(invoiceDetailInfo.getsContractType());
        		aaccountsReceivableAccorspandingContractService.getRepository().save(newAaccountsReceivableAccorspandingContract);
        	}else{
        		aaccountsReceivableAccorspandingContract.setnContractOrgType(2);
        		aaccountsReceivableAccorspandingContract.setnIsUpdate(0);
        		aaccountsReceivableAccorspandingContract.setnIsUpdateForTotal(0);
        		aaccountsReceivableAccorspandingContract.setsContractId(invoiceDetailInfo.getsContractId());
        		aaccountsReceivableAccorspandingContract.setsContractType(invoiceDetailInfo.getsContractType());
        		aaccountsReceivableAccorspandingContractService.getRepository().save(aaccountsReceivableAccorspandingContract);
        	}
            //invoiceDetailInfo1.setUpdateTime(new Date());
            //paymentInitialService.getPaymentRegistRepository().save(invoiceDetailInfo1);
            //map=paymentInitialService.updataPaymentPlan(paymentRegist,paymentRegist1,paymentSum);
            Account account = this.getMyAccount();
            User user = account.getUser();
            //payPlanService.payNotionalPooling(paymentRegist.getContractId(),user.getName());
            //paymentRegist.setUser(user);
            
            String contractId = invoiceDetailInfo.getsContractId();
            String contractType = invoiceDetailInfo.getsContractType();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            BigDecimal bigDecimal = new BigDecimal(invoiceDetailInfo.getnInvoiceDetailPrice());
            String content = "";
            if (contractType.equals("项目合同")){
    			Contract old = contractService.getRepository().findOne(contractId);
    			content = "编辑开票【"+"合同号:"+old.getSupplierCode()+",开票人"+name+",金额:"+bigDecimal.toString()+",时间:"+formatter.format(invoiceDetailInfo.getdInvoicePlanAccDate())+"】";
    		}else if (contractType.equals("备件合同")){
    			ContractPart old = contractService.getRepository2().findOne(contractId);
    			content = "编辑开票【"+"合同号:"+old.getSupplierCode()+",开票人"+name+",金额:"+bigDecimal.toString()+",时间:"+formatter.format(invoiceDetailInfo.getdInvoicePlanAccDate())+"】";
    		}else if (contractType.equals("备泵合同")){
    			ContractPump old = contractService.getRepository3().findOne(contractId);
    			content = "编辑开票【"+"合同号:"+old.getSupplierCode()+",开票人"+name+",金额:"+bigDecimal.toString()+",时间:"+formatter.format(invoiceDetailInfo.getdInvoicePlanAccDate())+"】";
    		}else if (contractType.equals("维修合同")){
    			ContractRepair old = contractService.getRepository4().findOne(contractId);
    			content = "编辑开票【"+"合同号:"+old.getSupplierCode()+",开票人"+name+",金额:"+bigDecimal.toString()+",时间:"+formatter.format(invoiceDetailInfo.getdInvoicePlanAccDate())+"】";
    		}
            operateLogService.log(user, "编辑开票", content);
            String paymentRegistId = invoiceDetailInfo.getId();
            Date createTime = invoiceDetailInfo.getCreateTime();
            float[] paymentSum=invoiceDetailInfoService.getRepository().getPaymentSum(contractId);
            float sums = 0;
            for (int i = 0; i < paymentSum.length; i++) {
                sums = sums + paymentSum[i];
            }
            map.put("code", 1);
            map.put("sum", sums);
            map.put("paymentRegistId", paymentRegistId);
            map.put("createTime", createTime);
            return map;
    }
    /**
     * 删除开票
     * @return
     */
    @RequestMapping(value ="/deletePayment",method = RequestMethod.GET)
    public Map<String,Object> deletePayment(HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
        String ids = request.getParameter("ids");
        String id[] = ids.split(",");
        List<InvoiceDetailInfo> invoiceDetailInfos = new ArrayList<>();
        /*List<PaymentPlan> changePaymentPaln=null;
        List<Date> paymentRegistList = new ArrayList<>();
        List<Date> getPaymentPaln=new ArrayList<>();*/
        String contractId=null;
        String contractType=null;
        for (int i = 0; i < id.length; i++){
            String paymentRegistId = id[i];
            InvoiceDetailInfo invoiceDetailInfo = new InvoiceDetailInfo();
            invoiceDetailInfo.setId(paymentRegistId);
            invoiceDetailInfos.add(invoiceDetailInfo);
            invoiceDetailInfo=invoiceDetailInfoService.getRepository().findOne(paymentRegistId);
            contractId=invoiceDetailInfo.getsContractId();
            contractType=invoiceDetailInfo.getsContractType();
        }
        invoiceDetailInfoService.getRepository().deleteInBatch(invoiceDetailInfos);
        AaccountsReceivableAccorspandingContract aaccountsReceivableAccorspandingContract = aaccountsReceivableAccorspandingContractService.getRepository().findBySContractIdAndSContractTypeAndNContractOrgType(contractId, contractType, 2);
        if(aaccountsReceivableAccorspandingContract == null){
    		AaccountsReceivableAccorspandingContract newAaccountsReceivableAccorspandingContract = new AaccountsReceivableAccorspandingContract();
    		newAaccountsReceivableAccorspandingContract.setnContractOrgType(2);
    		newAaccountsReceivableAccorspandingContract.setnIsUpdate(0);
    		newAaccountsReceivableAccorspandingContract.setnIsUpdateForTotal(0);
    		newAaccountsReceivableAccorspandingContract.setsContractId(contractId);
    		newAaccountsReceivableAccorspandingContract.setsContractType(contractType);
    		aaccountsReceivableAccorspandingContractService.getRepository().save(newAaccountsReceivableAccorspandingContract);
    	}else{
    		aaccountsReceivableAccorspandingContract.setnContractOrgType(2);
    		aaccountsReceivableAccorspandingContract.setnIsUpdate(0);
    		aaccountsReceivableAccorspandingContract.setnIsUpdateForTotal(0);
    		aaccountsReceivableAccorspandingContract.setsContractId(contractId);
    		aaccountsReceivableAccorspandingContract.setsContractType(contractType);
    		aaccountsReceivableAccorspandingContractService.getRepository().save(aaccountsReceivableAccorspandingContract);
    	}
        //paymentInitialService.getPaymentRegistRepository().deleteInBatch(paymentRegists);
        //Date firstDate=Collections.min(getPaymentPaln);
        //Date secondDate=Collections.min(paymentRegistList);
        //map=paymentInitialService.refreshPaymentPlan(firstDate,secondDate,contractId, contractType);
        /////插入删除日志
        Account a = this.getMyAccount();
        User user = a.getUser();
        String contractId1 = "";
        String contractType1 = "";
        for(InvoiceDetailInfo d:invoiceDetailInfos){
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            InvoiceDetailInfo oldPR=invoiceDetailInfoService.getRepository().findOne(d.getId());
            User sInvoicePerson = userService.findByEmpId(oldPR.getsInvoicePersonId());
            contractId1 = oldPR.getsContractId();
            contractType1 = oldPR.getsContractType();
            BigDecimal bigDecimal = new BigDecimal(oldPR.getnInvoiceDetailPrice());
            String content = "";
            if (contractType1.equals("项目合同")){
    			Contract old = contractService.getRepository().findOne(contractId1);
    			content = "删除开票【"+"合同号:"+old.getSupplierCode()+",开票人"+sInvoicePerson.getEmpLogin().getName()+",金额:"+bigDecimal.toString()+",时间:"+formatter.format(oldPR.getdInvoicePlanAccDate())+"】";
    		}else if (contractType1.equals("备件合同")){
    			ContractPart old = contractService.getRepository2().findOne(contractId1);
    			content = "删除开票【"+"合同号:"+old.getSupplierCode()+",开票人"+sInvoicePerson.getEmpLogin().getName()+",金额:"+bigDecimal.toString()+",时间:"+formatter.format(oldPR.getdInvoicePlanAccDate())+"】";
    		}else if (contractType1.equals("备泵合同")){
    			ContractPump old = contractService.getRepository3().findOne(contractId1);
    			content = "删除开票【"+"合同号:"+old.getSupplierCode()+",开票人"+sInvoicePerson.getEmpLogin().getName()+",金额:"+bigDecimal.toString()+",时间:"+formatter.format(oldPR.getdInvoicePlanAccDate())+"】";
    		}else if (contractType1.equals("维修合同")){
    			ContractRepair old = contractService.getRepository4().findOne(contractId1);
    			content = "删除开票【"+"合同号:"+old.getSupplierCode()+",开票人"+sInvoicePerson.getEmpLogin().getName()+",金额:"+bigDecimal.toString()+",时间:"+formatter.format(oldPR.getdInvoicePlanAccDate())+"】";
    		}
            operateLogService.log(user, "删除开票", content);
            payPlanService.payNotionalPooling(oldPR.getsContractId(),user.getName());
        }
        float[] paymentSum=invoiceDetailInfoService.getRepository().getPaymentSum(contractId);
        float sums = 0;
        for (int i = 0; i < paymentSum.length; i++) {
            sums = sums + paymentSum[i];
        }
        map.put("code", 1);
        map.put("sum", sums);
        return map;
    }
    /**
     * 开票历史tabs页面数据
     * @return
     */
    @RequestMapping(value = "/paymentHistory/{contractId}/{contractType}/getData",method = RequestMethod.GET)
    public Map<String,Object> paymentHistory(@PathVariable String contractId,
                                             @PathVariable String contractType){
        Map<String,Object> map=new HashMap<>();
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        List<InvoiceDetailInfoView> paymentRegistList=null;
        Long count=null;
        Account a = this.getMyAccount();
        String userId=a.getUser().getEmpId();
        paymentRegistList=invoiceDetailInfoViewService.getInvoiceDetailInfoHistory(start, length,contractId, contractType,userId,null,null);
        count=invoiceDetailInfoViewService.getInvoiceDetailInfoHistoryCount(contractId, contractType, userId);
        map.put("data", paymentRegistList);
        map.put("draw", request.getParameter("draw"));
        map.put("recordsFiltered", count);
        map.put("recordsTotal", count);
        return map;
    }
    /**
     * 开票历史tabs页面数据-财务
     * @return
     */
    @RequestMapping(value = "/paymentHistory/{contractId}/{contractType}/getAllData",method = RequestMethod.GET)
    public Map<String,Object> paymentAllHistory(@PathVariable String contractId,
                                             @PathVariable String contractType){
        Map<String,Object> map=new HashMap<>();
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        List<InvoiceDetailInfoView> paymentRegistList=null;
        Long count=null;
        Account a = this.getMyAccount();
        String userId=a.getUser().getId();
        paymentRegistList=invoiceDetailInfoViewService.getInvoiceDetailInfoAllHistory(start, length, contractId, contractType, null, null);
        count=invoiceDetailInfoViewService.getInvoiceDetailInfoAllHistoryCount(contractId, contractType);
        map.put("data", paymentRegistList);
        map.put("draw", request.getParameter("draw"));
        map.put("recordsFiltered", count);
        map.put("recordsTotal", count);
        return map;
    }
    /**
     * 回款历史tabs页面数据
     * @return
     */
    @RequestMapping(value = "/payHistory/{contractId}/{contractType}/getAllData",method = RequestMethod.GET)
    public Map<String,Object> payAllHistory(@PathVariable String contractId,
                                         @PathVariable String contractType){
        Map<String,Object> map=new HashMap<String,Object>();
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        List<ReceivedPaymentDetailInfoView> payRegistList=null;
        Long count=null;
        Account a = this.getMyAccount();
        String userId=a.getUser().getId();
        payRegistList= receivedPaymentDetailInfoViewService.getPayAllHistory(start, length, contractId, contractType, null, null);
        count=receivedPaymentDetailInfoViewService.getPayAllRegistCount(contractId);
        map.put("data", payRegistList);
        map.put("draw", request.getParameter("draw"));
        map.put("recordsFiltered", count);
        map.put("recordsTotal", count);
        return map;
    }
    /**
     * 回款历史tabs页面数据
     * @return
     */
    @RequestMapping(value = "/payHistory/{contractId}/{contractType}/getData",method = RequestMethod.GET)
    public Map<String,Object> payHistory(@PathVariable String contractId,
                                         @PathVariable String contractType){
        Map<String,Object> map=new HashMap<String,Object>();
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        List<ReceivedPaymentDetailInfoView> payRegistList=null;
        Long count=null;
        Account a = this.getMyAccount();
        String userId=a.getUser().getEmpLogin().getEmpId();
        payRegistList= receivedPaymentDetailInfoViewService.getPayHistory(start, length,contractId, contractType,userId,null,null);
        count=receivedPaymentDetailInfoViewService.getPayRegistCount(contractId,userId);
        map.put("data", payRegistList);
        map.put("draw", request.getParameter("draw"));
        map.put("recordsFiltered", count);
        map.put("recordsTotal", count);
        return map;
    }

    /**
     * 回款-计划tabs页面数据
     * @return
     */
    @RequestMapping(value = "/payPlan/{contractId}/{contractType}/getData",method = RequestMethod.GET)
    public Map<String,Object> payPlan(@PathVariable String contractId,
                                      @PathVariable String contractType){
        Map<String,Object> map=new HashMap<>();
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        List<PayPlan> payPlanList=null;
        Long count=null;
        payPlanList=paymentInitialService.getPayPlan(start, length,contractId, contractType,null,null);
        count=paymentInitialService.getPayPlanCount(contractId);
        map.put("data", payPlanList);
        map.put("draw", request.getParameter("draw"));
        map.put("recordsFiltered", count);
        map.put("recordsTotal", count);
        return map;
    }
    /**
     * 开票-计划tabs页面数据
     * @return
     */
    @RequestMapping(value = "/paymentPlan/{contractId}/{contractType}/getData",method = RequestMethod.GET)
    public Map<String,Object> paymentPlan(@PathVariable String contractId,
                                          @PathVariable String contractType){
        Map<String,Object> map=new HashMap<>();
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        List<PaymentPlan> paymentPlanList=null;
        Long count=null;
        paymentPlanList=paymentInitialService.getPaymentPlan(start, length,contractId, contractType,null,null);
        count=paymentInitialService.getPaymentPlanCount(contractId);
        map.put("data", paymentPlanList);
        map.put("draw", request.getParameter("draw"));
        map.put("recordsFiltered", count);
        map.put("recordsTotal", count);
        return map;
    }
    /**
     * 获取页面数据
     */
    @RequestMapping(value = "getpage", method = RequestMethod.GET)
    public Map<String, Object> getPage(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, String> condition = new HashMap<String, String>();
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        String key = request.getParameter("key");
        Account a = this.getMyAccount();
        String userId = a.getUser().getId();
        String superior = a.getUser().getSaleArea().getSuperior();
        String areaName = a.getUser().getSaleArea().getAreaName();
        String name = "paymentinfo";
        Module module = this.getPageModule(name);
        String dataFilter = module.getDataFilter();
        if (key == null) {
            key = "";
        }
        key = key.replaceAll("\\s+", "");
        if (userId == null) {
            userId = "";
        }
        if (superior == null) {
            superior = "";
        }
        if (areaName == null) {
            areaName = "";
        }
        if (dataFilter == null) {
            dataFilter = "";
        }
        condition.put("key", key);
        condition.put("userId", userId);
        condition.put("superior", superior);
        condition.put("areaName", areaName);
        condition.put("dataFilter", dataFilter);
        List<ContractInfo> contracts = contractService.getContractInfos(start, length, condition, null, null);
        long count = contractService.getContractInfosCount(condition);
        map.put("data", contracts);
        map.put("draw", request.getParameter("draw"));
        map.put("recordsFiltered", count);
        map.put("recordsTotal", count);
        return map;
    }
}
